package com.fitbet.wechat.web.controller.trading;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.entity.cfg.JsConfig;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.bet.UserBetStatus;
import com.fitbet.entity.trading.OrderType;
import com.fitbet.entity.trading.SalesOrder;
import com.fitbet.entity.user.User;
import com.fitbet.wechat.processor.WechatProcessor;
import com.fitbet.wechat.service.SalesOrderService;
import com.fitbet.wechat.service.UserBetService;
import com.fitbet.wechat.web.interceptor.UserContext;
import com.fitbet.wechat.web.interceptor.UserContextType;
import com.fitbet.wechat.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/shopping")
public class ShoppingController {
    
    @Autowired
    private SalesOrderService salesOrderService;
    
    @Autowired
    private WechatProcessor        wechatProcessor;
    
    @Autowired
    private UserBetService userBetService;
    
    /**
     * 竞猜/投注 支付准备页
     * 
     * @return
     */
    @RequestMapping("/prepare")
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView prepare(@Secure User user,
                                @RequestParam("id") Long id,
                                @RequestParam(name = "type", required = false, defaultValue = "bet") String type,
                                @RequestParam(name = "userBetId", required = false) Long userBetId,
                                @RequestParam(name = "support", required = false, defaultValue = "true") boolean support,
                                HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user);
        
        if(StringUtils.equalsIgnoreCase(type, "bet")) {
            modelAndView.setViewName("bet-shopping/shopping-index");
            // 当前可报名的期数
            modelAndView.addObject("betId", id);
        } else {
            modelAndView.setViewName("bet-shopping/support-shopping-index");
            UserBet userBet = userBetService.loadUserBet(id, new UserBetStatus[]{UserBetStatus.PROCESSING, UserBetStatus.PAID, UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE, UserBetStatus.CLOSEFAILED});
            modelAndView.addObject("userBet", userBet);
            modelAndView.addObject("supportId", id);
            modelAndView.addObject("support", support);
        }
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }
    
    /**
     * 竞猜/投注 支付成功页
     * 
     * @return
     */
    @RequestMapping("/result")
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView result(@Secure User user,
                               @RequestParam("orderId") Long orderId, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("bet-shopping/shopping-result");
        
        modelAndView.addObject("user", user);
        
        SalesOrder salesOrder = salesOrderService.load(SalesOrder.class, orderId);
        modelAndView.addObject("salesOrder", salesOrder);
        
        if(salesOrder.getType().equals(OrderType.BET_SUPPORT)) {
            modelAndView.setViewName("bet-shopping/support-shopping-result");
        }
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }

}
